import link from "axios";
import {To} from 'ls-base-lib'
import type {APIResponse, MasApiType} from "ls-base-lib/dist/src/library/AwaitTo/ToResult/types";
import {ModuleAuthorityType} from "../CopyCode/XAYL/GetButtonRight";
import {DataItemDetailType, SceneGetDetailResponse, UserInfoType} from "./xaylTypes";
import QueryString from "qs";

const axiosInstance = link.create();

const to = new To<MasApiType, 1>(x => {
  if (x.data.code === 200)
    return x.data.data;
  else
    to.showMessage(x.data.info);
});

if (baseAPI)
  axiosInstance.defaults.baseURL = baseAPI;
else
  console.log('未配置baseAPI，请检查');

const token = localStorage.getItem('TOKEN');
if (token) {
  axiosInstance.defaults.headers.common['token'] = token;
} else
  console.log('未检测到token，请检查');

/**---------------小安易联通用接口start---------------*/
//获取按钮权限
export async function getRightFromAPI(moduleCode: string) {
  const result = await to.async(axiosInstance.get<APIResponse<ModuleAuthorityType>>('/Module/GetModuleAuthority', {
    params: {moduleCode}
  }));
  return result.getInfo();
}

//获取场景
export async function getSceneDataAPI(ID: string) {
  const result = await to.async(axiosInstance.get<APIResponse<SceneGetDetailResponse>>('/XAYL_Designer/GisScene/GetDetail', {
    params: {ID}
  }));
  return result.getInfo();
}

//从小安易连获取token(免登）
export async function getTokenAPI() {
  const result = await to.async(
    axiosInstance.post<APIResponse<string>>("/User/Login", QueryString.stringify({
      //以下用户名和密码需要根据实际情况替换
      username: 'masapi',
      password: 'cec74192ad15aba40b01a0f07e407fb7'
    })));
  return result.getInfo();
}

/**
 * 获取当前登录用户信息
 */
export async function getCurrentUserAPI() {
  const result = await to.async(axiosInstance.get<APIResponse<UserInfoType>>('/User/Current'));
  return result.getInfo();
}

//获取枚举
export async function getDataItemListAPI(itemCode: string) {
  const result = await to.async(axiosInstance.get<APIResponse<DataItemDetailType[]>>('/DataItem/GetDetailList', {
    params: {itemCode}
  }));
  return result.getInfo();
}
/**---------------小安易联通用接口end---------------*/
